import http from "@/utils/http";

const API_URL = process.env.REACT_APP_APIURL;
export const FILE_DOWNLOAD_URL = API_URL + "/file/download/path/";
export const FILE_UPLOAD_SPEED_URL = API_URL + "/file/upload/speed";

export function getInfo(filePath = "") {
  const query = filePath ? `?path=${filePath}` : "";
  return http.get(`/file/info${query}`);
}

export function getFileInfo(filePath) {
  return http.get(`/file/detail?path=${filePath}`);
}

export function createDownload(filePath) {
  return http.get(`/file/download/create?path=${filePath}`);
}

// export function getFileUrl(url) {
//   return new Promise((resolve) => {
//     http.get(`/profile${url}`, { responseType: "blob" }).then((res) => resolve(URL.createObjectURL(res)));
//   });
// }

/**
 * 文件重命名
 *
 * @param {String} filePath 文件路径
 * @param {String} fileName 新文件名
 * @returns
 */
export function fileRename(filePath, fileName) {
  return http.post("/file/rename", { filePath, fileName });
}

/**
 * 文件上传
 *
 * @param {String} dir 保存路径
 * @param {File} file 文件
 * @returns
 */
export function fileUpload(dir, file) {
  return http.post("/file/upload/single", { dir, file }, { headers: { "Content-Type": "multipart/form-data" } });
}

/**
 * 文件分片上传
 *
 * @param {Object} data
 * @returns
 */
export function fileSliceUploadCheck(param) {
  return http.get("/file/upload/slice/check?path=" + param);
}

/**
 * 文件分片上传
 *
 * @param {Object} data
 * @returns
 */
export function fileSliceUpload(data, { onUploadProgress }) {
  const startTime = Date.now();
  return http.post("/file/upload/slice", data, {
    headers: { "Content-Type": "multipart/form-data" },
    onUploadProgress: (res) => onUploadProgress && onUploadProgress({ ...res, costTime: Date.now() - startTime }),
  });
}

/**
 * 文件分片合并
 *
 * @param {Object} data
 * @returns
 */
export function fileSliceMerge(data) {
  return http.post("/file/upload/slice/merge", data);
}

/**
 * 分片合并验证
 *
 * @param {String} taskId 任务ID
 * @returns
 */
export function fileSliceMergeVerify(taskId) {
  return http.get("/file/upload/slice/merge/verify/" + taskId);
}
